import { createSlice } from '@reduxjs/toolkit'

interface ParkingState {
  spaces: any[]
  reservations: any[]
  statistics: any
  loading: boolean
  error: string | null
}

const initialState: ParkingState = {
  spaces: [],
  reservations: [],
  statistics: {},
  loading: false,
  error: null,
}

const parkingSlice = createSlice({
  name: 'parking',
  initialState,
  reducers: {
    setSpaces: (state, action) => {
      state.spaces = action.payload
    },
    updateSpace: (state, action) => {
      const index = state.spaces.findIndex(s => s.id === action.payload.id)
      if (index !== -1) {
        state.spaces[index] = { ...state.spaces[index], ...action.payload }
      }
    },
    setReservations: (state, action) => {
      state.reservations = action.payload
    },
    setStatistics: (state, action) => {
      state.statistics = action.payload
    },
  },
})

export const { setSpaces, updateSpace, setReservations, setStatistics } = parkingSlice.actions
export default parkingSlice.reducer
